﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class ManageEdit : PowerBase
    {
        CJJCH.BLL.T_NET_Manager bll_manager = new BLL.T_NET_Manager();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultButton = "btnSave";
            if (!IsPostBack)
            {
                InitRequest();
                InitControl();
                ShowManager();
            }

            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }

        private void InitRequest()
        {
            if (Request.QueryString["ID"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].Trim()))
            {
                this.lblID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ID"].ToString());
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("错误操作！");
            }
            if (Request.QueryString["keyword"]!=null)
            {
                this.lblkeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].ToString());
            }
        }

        private void InitControl()
        {
            lnkManagerList.NavigateUrl = "ManageList.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(this.lblkeyword.Text.Trim());

        }

        private void ShowManager()
        {
            CJJCH.Model.T_NET_Manager model = bll_manager.GetManagerModel(new Guid(this.lblID.Text.Trim()));
            if (model != null)
            {
                this.lblUserName.Text = model.UserName;
                this.txtRealName.Text = model.RealName;
                this.txtUserTel.Text = model.UserTel;
                this.lblUserPwd.Text = model.UserPwd;
                model = null;
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("错误操作！");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = CheckInput();
            if (error == "")
            {
                CJJCH.Model.T_NET_Manager model = new CJJCH.Model.T_NET_Manager();
                model.ID = new Guid(lblID.Text.Trim());
                model.BuildDate = DateTime.Now;
                model.ManagerID = SessionManagerID;
                model.RealName = LTP.Common.StringClass.EncodeString(this.txtRealName.Text.Trim());
                model.UserName = LTP.Common.StringClass.EncodeString(this.lblUserName.Text.Trim());
                if (this.txtUserPw1d.Text.Trim() == "")
                {
                    model.UserPwd = this.lblUserPwd.Text.Trim();
                }
                else
                {
                    model.UserPwd = LTP.Common.StringClass.EncryptPassword(txtUserPw1d.Text, LTP.Common.StringClass.PasswordFormat.MD5_32);
                }
                model.UserTel = LTP.Common.StringClass.EncodeString(this.txtUserTel.Text.Trim());
                error = bll_manager.UpdateManager(model);
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }

            if (error == "")
            {
                LTP.Common.WindowClass.WindowGoHref("管理员信息修改完成！", "ManageList.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim()) + "");
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            LTP.Common.WindowClass.WindowGoHref("ManageList.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim()) + "");
        }

        private string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtUserPw1d.Text != txtUserPw2d.Text)
            {
                error.Append("两次输入的密码不一致！\\n");
            }
            if (txtRealName.Text.Trim() == "")
            {
                error.Append("姓名不能为空！\\n");
            }
            if (txtUserTel.Text.Trim() == "")
            {
                error.Append("联系电话不能为空！\\n");
            }
            return error.ToString();
        }

    }
}